//
//  WWButtonView.m
//  Shopping
//
//  Created by 张湾湾 on 16/8/19.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#import "WWIconView.h"
#import "WWNetworkTools.h"
#import "WWButton.h"
#import <UIButton+WebCache.h>
#import "WWIconModel.h"
#import "WWIconViewController.h"
#import "WWMiddleButton.h"
@interface WWIconView()
@property (nonatomic,strong)NSMutableArray *buttons;

@property (nonatomic,strong)WWButton *button;
@end
@implementation WWIconView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - 赋值 -
- (void)setIconModel:(NSArray<WWIconModel *> *)iconModel{
    _iconModel = iconModel;
    [self.iconModel enumerateObjectsUsingBlock:^(WWIconModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        WWButton * button= self.buttons[idx];
        [button sd_setImageWithURL:[NSURL URLWithString:obj.img] forState:UIControlStateNormal];
        [button setTitle:obj.name forState:UIControlStateNormal];
        [button sizeToFit];
        
    }];
}
#pragma mark - 点击事件 -
- (void)didClickButton:(WWButton *)sender{
     WWIconModel * model = self.iconModel[sender.tag];
    
    if (self.returnblk) {
        self.returnblk(model.customURL);
    }
    
}

- (void)setupUI{
    //位置
    for (int i = 0; i < 4; i++) {
        WWMiddleButton * button = [WWMiddleButton new];
        button.tag = i;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:button];
        [button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttons addObject:button];
    }
}


-(void)layoutSubviews{
    CGFloat buttonW = SCREENW / 4;
    for (int i = 0; i < self.buttons.count; i++) {
        CGFloat buttonX = i * buttonW;
        WWMiddleButton *btn = self.buttons[i];
        
        btn.frame = CGRectMake(buttonX,0, buttonW, self.frame.size.height);
    }
}


- (NSMutableArray  *)buttons{
    if (!_buttons) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}
@end
